package com.example.mvvmtemplate.utli;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TimeUtil {

    /**
     * 获取当前时间
     */
    public static String getCurrentTime() {
        return getCurrentTime("yyyy-MM-dd");
    }

    /**
     * 获取当前时间
     */
    public static String getCurrentTime(String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, Locale.getDefault());// HH:mm:ss
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }

    /**
     * 获取当前时间
     */
    public static String getCalendarTime() {
        return getCalendarTime(0);
    }

    /**
     * 获取当前时间
     */
    public static String getCalendarTime(int field) {
        Calendar calendar = Calendar.getInstance();
        if (field == 1) {
            int year = calendar.get(Calendar.YEAR);
            return year + "";
        } else if (field == 2) {
            int month = calendar.get(Calendar.MONDAY);
            int mMonth = month + 1;
            return mMonth + "";
        } else if (field == 5) {
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            return day + "";
        } else {
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONDAY);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            int mMonth = month + 1;
            return year + "-" + mMonth + "-" + day;
        }
    }

    /**
     * 获取当前date+month月的时间
     *
     * @param dateStr 传入的日期
     */
    public static String getMonthTime(String dateStr, int month) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            Date date = simpleDateFormat.parse(dateStr);
            return getMonthTime(date, month);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取当前date+month月的时间
     *
     * @param date 传入的日期
     */
    public static String getMonthTime(Date date, int month) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取本周的起始时间
     */
    public static long getTimeOfWeekStart() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.clear(Calendar.MINUTE);
        ca.clear(Calendar.SECOND);
        ca.clear(Calendar.MILLISECOND);
        ca.set(Calendar.DAY_OF_WEEK, ca.getFirstDayOfWeek() + 1);
        return ca.getTimeInMillis();
    }

    /**
     * 获取本月的起始时间
     */
    public static long getTimeOfMonthStart() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.clear(Calendar.MINUTE);
        ca.clear(Calendar.SECOND);
        ca.clear(Calendar.MILLISECOND);
        ca.set(Calendar.DAY_OF_MONTH, 1);
        return ca.getTimeInMillis();
    }

    /**
     * 获取本年的起始时间
     */
    public static long getTimeOfYearStart() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.clear(Calendar.MINUTE);
        ca.clear(Calendar.SECOND);
        ca.clear(Calendar.MILLISECOND);
        ca.set(Calendar.DAY_OF_YEAR, 1);
        return ca.getTimeInMillis();
    }
}
